package com.xhji.spider.examples;

import lombok.Data;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.ExtractByUrl;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.stream.Stream;

@TargetUrl("https://gitee.com/\\w+/\\w+")
@HelpUrl("https://gitee.com/explore/all\\?lang=Java\\d+")
@Data
public class Oschina {

    @ExtractBy(value = "//a[@class='repository']/text()", notNull = true)
    private String name;

    @ExtractByUrl("https://gitee\\.com/(\\w+)/.*")
    private String author;

//    @ExtractBy("//div[@class='file_content markdown-body']/tidyText()")
//    private String readme;

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
//        OOSpider.create(Site.me().setSleepTime(1000)
//                , new ConsolePageModelPipeline(), Oschina.class)
//                .addUrl("https://gitee.com/alvis-yu/uexam").thread(5).run();

        Oschina os = new Oschina();
        TargetUrl annotation = os.getClass().getAnnotation(TargetUrl.class);
        Stream.of(annotation.value()).forEach(System.out::println);
        InvocationHandler h = Proxy.getInvocationHandler(annotation);
        Field hField = h.getClass().getDeclaredField("memberValues");
        // 因为这个字段事 private final 修饰，所以要打开权限
        hField.setAccessible(true);
        // 获取 memberValues
        Map memberValues = (Map) hField.get(h);
        // 修改 value 属性值
        memberValues.put("value", new String[]{"133"});
        Stream.of(annotation.value()).forEach(System.out::println);
    }
}
